﻿namespace NEvoWeb.Modules.NB_Store
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class BaseDisplayTemplate : ITemplate
    {
        protected string[] _aryTempl;
        protected string _TemplateText;

        public BaseDisplayTemplate(string TemplateText)
        {
            this._aryTempl = this.ParseTemplateText(TemplateText);
            this._TemplateText = TemplateText;
        }

        internal object assignProperties(string TagString, object obj, string IdPrefix = "")
        {
            string[] strArray = TagString.Split(new char[] { ':' });
            NewLateBinding.LateSet(obj, null, "ID", new object[] { IdPrefix + strArray[1].ToLower() }, null, null);
            if (strArray.GetUpperBound(0) >= 2)
            {
                obj = RuntimeHelpers.GetObjectValue(SharedFunctions.assignByReflection(RuntimeHelpers.GetObjectValue(obj), strArray[2]));
            }
            return obj;
        }

        private void ClassDataBind_DataBinding(object sender, EventArgs e)
        {
            Literal literal = (Literal) sender;
            DataListItem namingContainer = (DataListItem) literal.NamingContainer;
            literal.Text = DataBinder.Eval(RuntimeHelpers.GetObjectValue(namingContainer.DataItem), literal.Text).ToString();
        }

        public virtual void InstantiateIn(Control container)
        {
            int upperBound = this._aryTempl.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                if (this._aryTempl[i].ToUpper().StartsWith("TAG:LINKBUTTON"))
                {
                    LinkButton button = new LinkButton();
                    this.assignProperties(this._aryTempl[i], button, "");
                    button.DataBinding += new EventHandler(this.LinkButtonDataBind_DataBinding);
                    container.Controls.Add(button);
                }
                else
                {
                    Literal literal;
                    if (this._aryTempl[i].ToUpper().StartsWith("TAG:DATABIND"))
                    {
                        string[] strArray = this._aryTempl[i].Split(new char[] { ':' });
                        literal = new Literal {
                            Text = strArray[2]
                        };
                        literal.DataBinding += new EventHandler(this.ClassDataBind_DataBinding);
                        container.Controls.Add(literal);
                    }
                    else
                    {
                        literal = new Literal {
                            Text = this._aryTempl[i],
                            EnableViewState = false
                        };
                        container.Controls.Add(literal);
                    }
                }
            }
        }

        private void LinkButtonDataBind_DataBinding(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton) sender;
            DataListItem namingContainer = (DataListItem) button.NamingContainer;
            button.CommandArgument = DataBinder.Eval(RuntimeHelpers.GetObjectValue(namingContainer.DataItem), button.CommandArgument).ToString();
        }

        internal string[] ParseTemplateText(string TemplText)
        {
            char[] separator = new char[] { '[', ']' };
            return TemplText.Split(separator);
        }
    }
}

